<?php

class SiteController extends TickGoalsController
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	public function actionMyGoals()
	{
		
		$this->render('my_goals',array());
	}
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$this->layout = "main";
		
		
		
		
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index',array());
	}

	public function actionLt(){
		$this->render('test',array());
	}
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		$this->layout = "main";
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	
	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionRegisterUser()
	{
		$model=new Users;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['Users']))
		{
			$model->attributes=$_POST['Users'];
			$groupUser = Groups::model()->notAdmin();
			if(empty($groupUser)){
				$errors = $model->getErrors();
				list ($field, $_errors) = each ($errors);
				throw new Exception($_errors[0]);
			}
			$model->group_id = $groupUser->id;
			if($model->validate()){
				$model->password = Users::model()->encodePassword($model->password);
				$model->created = date("Y-m-d G:i:s");
				$model->birth = $model->created;
				if($model->save()){
					Yii::app()->user->setFlash('success', 'Account created successfully.');
					$this->redirect(array('/site'));
				}else{
					$errors = $model->getErrors();
					list ($field, $_errors) = each ($errors);
					throw new Exception($_errors[0]);
				}
			}else{
				$errors = $model->getErrors();
				list ($field, $_errors) = each ($errors);
				throw new Exception($_errors[0]);
			}
		}
		// display the login form
		
	}
	public function actionForgot(){
		$this->render('forgot');
	}
	public function actionShareMail($url=NULL,$title=NULL){
		$model = new ShareEmail;
		
		if(!empty($_POST)){
			$model->attributes = $_POST['ShareEmail'];
			if($model->validate()){
		// Yii::import('ext.yii-mail.*');
		// $message = new YiiMailMessage;
		$first_name = currentUser()->first_name;		
		$subject = "{$first_name} has shared their goal list with you!";
		$to      = $model->email;
				
	
	
$message = 'Hi

Check out '.$first_name.' goal list '.$title.' here:
'.$url.'
Brought to you by TickGoals.com

Thanks';
				$headers = "From: " .Yii::app()->params['adminEmail']. "\r\n" .
					'Reply-To:' . Yii::app()->params['adminEmail']."\r\n" .
					'X-Mailer: PHP/' . phpversion();

				mail($to, $subject, $message, $headers);
		// $message->view = 'sharemail';
		//userModel is passed to the view
		// $message->setSubject($subject);
		// $message->setBody(array(
			// 'first_name'=>$first_name,
			// 'url'=>$url,
			// 'title'=>$title,
		// ), 'text/html');
		// $message->addTo(currentUser()->email);
		// $message->from = Yii::app()->params['adminEmail'];
		// Yii::app()->mail->send($message);
		Yii::app()->user->setFlash('success','Share '.$first_name.' goal list My Project successfully.');
		$this->redirect('/site');
	
		}
		}
		
		$this->render('share',array(
			'title'=>$title,
			'url'=>$url,
			'model'=>$model,
		));
		
		
	}
	public function actionCron(){
		
		/*$to      = 'thinhpq@appdev.vn';

		$message = "Hi
		Your password for TickGoals.com is \r\n\r\n Thanks ";
		$headers = "From: " .Yii::app()->params['adminEmail']. "\r\n" .
			'Reply-To:' . Yii::app()->params['adminEmail']."\r\n" .
			'X-Mailer: PHP/' . phpversion();

		mail($to, 'test', $message, $headers);	*/
		$users = Users::model()->findAll();
		
if(!empty($users)){
	foreach($users as $key=>$u){
		$lists = Lists::model()->findAllByAttributes(array(
			'user_id'=>$u->id
		));
		$first_name= $u->first_name;
		$to = "quocthinh9889@gmail.com";
		if(!empty($lists)){
			foreach($lists as $kl=>$l){
				$goals =  Goals::model()->findAllByAttributes(array(
					'list_id'=>$l->id,
					'reminder'=>'Daily'
				));
				
				if(!empty($goals)){
					foreach($goals as $kg=>$g){
						$subject = "Reminder: {$g->name}";
						

						

						$message = 'Hi '.$first_name.' 

	Just a reminder your goal:
						
	'.$g->name.' is due on the '.$g->completion.'
						
						
						
	Brought to you by TickGoals.com (link to TickGoals.com)

Thanks';
						$headers = "From: " .Yii::app()->params['adminEmail']. "\r\n" .
							'Reply-To:' . Yii::app()->params['adminEmail']."\r\n" .
							'X-Mailer: PHP/' . phpversion();

						mail($to, $subject, $message, $headers);
					}
				}
			}
		}
	}
}
		dump($_SERVER['DOCUMENT_ROOT']);
	}
	
}
